{{- define "csi_controller_args" }}
- "--nodeid=$(NODE_ID)"
- "--cloud-config=/etc/config/cloud-config"
- "--cluster={{ .Values.global.discovery.clusterUUID }}"
- "--endpoint=unix:///csi/csi.sock"
{{- end }}

{{- define "csi_controller_envs" }}
- name: NODE_ID
  valueFrom:
    fieldRef:
      fieldPath: spec.nodeName
{{- include "helm_lib_envs_for_proxy" . }}
{{- end }}

{{- define "csi_controller_volumes" }}
- name: secret-cinderplugin
  secret:
    secretName: cloud-controller-manager
{{- end }}

{{- define "csi_controller_volume_mounts" }}
- name: secret-cinderplugin
  mountPath: /etc/config
  readOnly: true
{{- end }}

{{- define "csi_node_args" }}
- "--nodeid=$(NODE_ID)"
- "--cloud-config=/etc/config/cloud-config"
- "--endpoint=unix:///csi/csi.sock"
{{- end }}

{{- define "csi_node_envs" }}
- name: NODE_ID
  valueFrom:
    fieldRef:
      fieldPath: spec.nodeName
{{- include "helm_lib_envs_for_proxy" . }}
{{- end }}

{{- define "csi_node_volumes" }}
- name: secret-cinderplugin
  secret:
    secretName: cloud-controller-manager
{{- end }}

{{- define "csi_node_volume_mounts" }}
- name: secret-cinderplugin
  mountPath: /etc/config
  readOnly: true
{{- end }}

{{- $kubernetesSemVer := semver .Values.global.discovery.kubernetesVersion }}
{{- $csiImageName := join "" (list "cinderCsiPlugin" $kubernetesSemVer.Major $kubernetesSemVer.Minor ) }}
{{- $csiControllerImage := include "helm_lib_module_image_no_fail" (list . $csiImageName) }}
{{- if $csiControllerImage }}

  {{- $csiControllerConfig := dict }}
  {{- $_ := set $csiControllerConfig "controllerImage" $csiControllerImage }}
  {{- $_ := set $csiControllerConfig "topologyEnabled" .Values.cloudProviderOpenstack.storageClass.topologyEnabled }}
  {{- $_ := set $csiControllerConfig "additionalControllerArgs" (include "csi_controller_args" . | fromYamlArray) }}
  {{- $_ := set $csiControllerConfig "additionalControllerEnvs" (include "csi_controller_envs" . | fromYamlArray) }}
  {{- $_ := set $csiControllerConfig "additionalControllerVolumes" (include "csi_controller_volumes" . | fromYamlArray) }}
  {{- $_ := set $csiControllerConfig "additionalControllerVolumeMounts" (include "csi_controller_volume_mounts" . | fromYamlArray) }}

  {{- include "helm_lib_csi_controller_manifests" (list . $csiControllerConfig) }}

  {{- $csiNodeConfig := dict }}
  {{- $_ := set $csiNodeConfig "nodeImage" $csiControllerImage }}
  {{- $_ := set $csiNodeConfig "driverFQDN" "cinder.csi.openstack.org" }}
  {{- $_ := set $csiNodeConfig "additionalNodeArgs" (include "csi_node_args" . | fromYamlArray) }}
  {{- $_ := set $csiNodeConfig "additionalNodeEnvs" (include "csi_node_envs" . | fromYamlArray) }}
  {{- $_ := set $csiNodeConfig "additionalNodeVolumes" (include "csi_node_volumes" . | fromYamlArray) }}
  {{- $_ := set $csiNodeConfig "additionalNodeVolumeMounts" (include "csi_node_volume_mounts" . | fromYamlArray) }}

  {{- include "helm_lib_csi_node_manifests" (list . $csiNodeConfig) }}
{{- end }}
